package com.ubix.kiosoft2.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;

import com.ubix.kiosoft2.R;

/**
 * Created by Chris.Choi on 3/15/2017.
 */

public class CommonDialog {
    final static String TAG = "CommonDialog";
    private static AlertDialog vDialog;
    public static void openSingleDialog(Context context,String title, String msg){
        openSingleDialog(context,title,msg,null);
    }

    public static void openSingleDialog(Context context, String title, String msg, @Nullable DialogInterface.OnDismissListener dismissListener){
        try {
            if((vDialog != null &&vDialog.isShowing())){
                vDialog.dismiss();
            }
        } catch (Exception e) {
            if (context != null && context.getClass() != null) Log.i("robin", "context==" + context.getClass().getSimpleName());
            Log.i("robin", "openSingleDialog==>catch");
            e.printStackTrace();
        }
        if(vDialog == null || (vDialog != null && !vDialog.isShowing())) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setCancelable(false);
            builder.setTitle(title);
            builder.setMessage(msg);
            builder.setPositiveButton(R.string.dialog_ok, null);
            if (dismissListener != null) builder.setOnDismissListener(dismissListener);
            vDialog = builder.create();
            try {
                Log.i("robin", "openSingleDialog: vDialog.show();");
                vDialog.show();
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }else{
            Log.i("robin", "else    vDialog=="+vDialog);
        }
    }

    public static void openDoubleDialog(Context context, int title, int msg,
                                        DialogInterface.OnClickListener positiveBtnListener,
                                        DialogInterface.OnClickListener negativeBtnListener){
        openDoubleDialog(context,title,msg,positiveBtnListener,negativeBtnListener,null);
    }

    public static void openDoubleDialog(Context context, int title, int msg,
                                        DialogInterface.OnClickListener positiveBtnListener,
                                        DialogInterface.OnClickListener negativeBtnListener,
                                        @Nullable View view){
        try {
            if((vDialog != null &&vDialog.isShowing())){
                vDialog.dismiss();
            }
        } catch (Exception e) {
            Log.i("robin", "openDoubleDialog==>catch");
            e.printStackTrace();
        }
        if(vDialog == null || (vDialog != null && !vDialog.isShowing())) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setMessage(msg);
            builder.setCancelable(false);
            if (view != null) builder.setView(view);
            builder.setPositiveButton(context.getText(R.string.dialog_ok), positiveBtnListener);
            builder.setNegativeButton(context.getText(R.string.dialog_cancel), negativeBtnListener);
            vDialog = builder.create();
            try {
                vDialog.show();
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }else{
            Log.i("robin", "else    vDialog=="+vDialog);
        }
    }

    public static void openDoubleDialog(Context context, String positiveBtnStr, String negativeBtnStr, String title, String msg,
                                        DialogInterface.OnClickListener positiveBtnListener,
                                        DialogInterface.OnClickListener negativeBtnListener,
                                        @Nullable View view) {
        try {
            if((vDialog != null &&vDialog.isShowing())){
                vDialog.dismiss();
            }
        } catch (Exception e) {
            Log.i("robin", "openDoubleDialog==>catch");
            e.printStackTrace();
        }
        if (vDialog == null || (vDialog != null && !vDialog.isShowing())) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setMessage(msg);
            builder.setCancelable(false);
            if (view != null) builder.setView(view);
            builder.setPositiveButton(positiveBtnStr, positiveBtnListener);
            builder.setNegativeButton(negativeBtnStr, negativeBtnListener);
            vDialog = builder.create();
            try {
                vDialog.show();
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }else{
            Log.i("robin", "else    vDialog=="+vDialog);
        }
    }
}
